package com.example.expresscabinet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.expresscabinet.common.Result;
import com.example.expresscabinet.dao.friendMapper;
import com.example.expresscabinet.dao.getexpressMapper;
import com.example.expresscabinet.model.friend;
import com.example.expresscabinet.model.friendetail;
import com.example.expresscabinet.service.ICabinetService;
import com.example.expresscabinet.service.IFriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
public class friendServiceImpl extends ServiceImpl<friendMapper,friend> implements IFriendService {
    @Resource
    friendMapper friendMapper;

    @Resource
    getexpressMapper getexpressMapper;

    @Autowired
    ICabinetService cabinetService;

    @Override
    public Result<?> addFriendMsg(String receiver,String sender,String code){
        QueryWrapper<friend> wrapper=new QueryWrapper<>();
        wrapper.eq("receiver",receiver).eq("sender",sender).eq("code",code);
        friend friend=friendMapper.selectOne(wrapper);
        Result res=new Result();
        if(friend!=null){
            res.setCode("-1");
            res.setMsg("请求重复发送");
            return res;
        }
        friend=new friend();
        friend.setCode(code);
        friend.setReceiver(receiver);
        friend.setSender(sender);
        friend.setStates("未取走");
        friendMapper.insert(friend);
        res.setCode("0");
        res.setMsg("成功");
        return res;
    }

    @Override
    public void deleteFriendMsg(String receiver, String code){
        //List<friend> friendList=friendMapper.queryAllFriend("code");
        QueryWrapper<friend> wrapper=new QueryWrapper<>();
        wrapper.eq("code",code);
        friend friend=new friend();
        friend.setTaker(receiver);
        friend.setStates("已取走");
        friendMapper.update(friend,wrapper);
    }

    @Override
    public Page<friendetail> queryNoTake(String receiver, int pageNum, int pageSize){
        Page<friendetail> friendList=friendMapper.queryNotake(new Page<>(pageNum,pageSize),receiver,"未取走");
        return friendList;
    }

    @Override
    public Page<friendetail> queryTake(String receiver,int pageNum,int pageSize){
        Page<friendetail> friendList=friendMapper.queryTake(new Page<>(pageNum,pageSize),receiver,"已取走");
        return friendList;
    }

}
